import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Arrays;

public class ArrayBlog {
    public static void main(String[] args) {

        //数组创建方式及初始化
        //1.创建方式（推荐）
//        数据类型[] 数组名; 比如：int[] arr;
        //C语言风格 ×
//        数据类型 数组名[];比如 int arr[];

        //静态初始化：数据类型[] 数组名 = {data1，data2，data3...datan};

        int[] array1 = {1,2,3,4,5,6,7};
        double[] array2 = {1.0, 2.0, 3.4, 5.6};
        char[] array3 = {'1','A','a','对'};
        boolean[] array4 = {true,false};
        String[] array5 = {"hello","WORLD","123","!!!","你好"};

        //数据类型[] 数组名 = new 数据类型[] {data1,data2,data3...datan};

        int[] array6 = new int[]{1,2,3,4,5,6,7};
        double[] array7 = new double[]{1.0, 2.0, 3.4, 5.6};
        char[] array8 = new char[]{'1','A','a','对'};
        boolean[] array9 = new boolean[]{true,false};
        String[] array10 = new String[]{"hello","WORLD","123","!!!","你好"};

        //数据类型[] 数组名 = new 数据类型[元素个数];
        int[] array11 = new int[7]; // 动态创建数组
        array11[0] = 1; // 手动赋值
        double[] array12 = new double[4];
        array12[2] = 2.3;
        char[] array13 = new char[4];
        array13[1] = 'a';
        boolean[] array14 = new boolean[2];
        array14[1] = true;
        String[] array15 = new String[5];
        array15[2] = "aaa";


        //分两步
        int[] arr1;
        arr1 = new int[10];

        int[] arr2;
        arr2 = new int[]{1,2,3,4,5,6,7};

        int[] arr3;
        arr3 = {1,2,2,3,4};

        int[] array1 = {1,2,3,4,5,6,7};
        System.out.println(array1);


        int[]array = new int[]{10, 20, 30, 40, 50};
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[3]);
        System.out.println(array[4]);
        // 也可以通过[]对数组中的元素进行修改
        array[0] = 100;
        System.out.println(array[0]);
        System.out.println("===============");
        System.out.println(Arrays.toString(array));

       /* int[] array1 = {1, 2, 3};
        System.out.println(array1[3]); // 数组中只有3个元素，下标一次为：0 1 2，array[3]下标越界
// 执行结果
        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
        at Test.main(Test.java:4)*/

        int[] array = new int[]{10, 20, 30, 40, 50};
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[3]);
        System.out.println(array[4]);

        //上述代码可以起到对数组中元素遍历的目的，但问题是：
        //1. 如果数组中增加了一个元素，就需要增加一条打印语句
        //2. 如果输入中有100个元素，就需要写100个打印语句
        //3. 如果现在要把打印修改为给数组中每个元素加1，修改起来非常麻烦。
        //通过观察代码可以发现，对数组中每个元素的操作都是相同的，则可以使用循环来进行打印

        for (int i = 0; i < 5; i++) {
            System.out.println(array[i]);
        }
        //数组对象.length
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

        /*for (int num : arr) {
            System.out.println(num);
        }*/

        for (int num : array) {
            System.out.println(num);
        }




    }
}
